package o2o.web.actions.report;

import com.jinyou.utils.common.AjaxResult;
import o2o.config.dao.DataDaoSql;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.report.ShopReportStatistics;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class ShopReportStatisticsAction {

    //  店铺统计
    @RequestMapping(method = RequestMethod.POST, value = "/admin/report/shop/statistics")
    @ResponseBody
    public String tableSelect(String city, String county,
                              Long startTime, Long endTime,
                              Integer page, Integer size,
                              String role, String day
    ) {

        if (!"1".equals(role) && !"2".equals(role)) {
            role = "1";
        }

        if (startTime == null) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -1);
            Date time = cal.getTime();
            startTime = time.getTime();
        }
        if (endTime == null) {
            endTime = new Date().getTime();
        }
        if ("1".equals(day)) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -1);
            Date time = cal.getTime();
            startTime = time.getTime();
            endTime = new Date().getTime();
        }
        if ("7".equals(day)) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -7);
            Date time = cal.getTime();
            startTime = time.getTime();
            endTime = new Date().getTime();
        }
        if ("30".equals(day)) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -30);
            Date time = cal.getTime();
            startTime = time.getTime();
            endTime = new Date().getTime();
        }
        page = (page - 1) * size;
        List<ShopReportStatistics> returnShopReportStatistics = new ArrayList<ShopReportStatistics>();
        // 从数据库中提取
        List<ShopReportStatistics> shopReportStatisticsList = getShopReportfromDataBase(city, page, size, startTime, endTime, role);


        //  判断条件
        /*for (ShopReportStatistics shopReportStatistics : shopReportStatisticsList) {
            if ("".equals(county)) {
                if (shopReportStatistics.getCity().equals(city)) {
                    returnShopReportStatistics.add(shopReportStatistics);
                }
            } else if (shopReportStatistics.getCity().equals(city) && shopReportStatistics.getCountry().equals(county)) {
                returnShopReportStatistics.add(shopReportStatistics);
            }
        }*/
        long totalcount = page(city, role, startTime, endTime);
        if (totalcount == 0) {
            totalcount = 1l;
        }
        return AjaxResult.successResult(totalcount, shopReportStatisticsList.size(), shopReportStatisticsList);
    }

    // 分页查询
    private List<ShopReportStatistics> getShopReportfromDataBase(String city, Integer page, Integer size, long startTime, Long endTime, String role) {
        StringBuffer sb = new StringBuffer();

        //  获取符合role  createtime  条件的统计数据
        // SELECT id,shopName,shopRole,SUM(orderCount),SUM(ShopTurnover),createtime,city,country FROM
        //     shop_report_statistics WHERE shoprole=1 AND createtime>= 1545899111112 &&
        //        createtime<=1545899111122
        //         GROUP BY shopName
        sb.append("SELECT id,shopName,shopRole,SUM(successorderCount) AS successOrderCount,SUM(ShopTurnover) AS shopTurnover,createtime,city,country FROM ")
                .append(PojoTables.SHOP_REPORT_STATISTICS).append(" where shoprole= ").append(role).append(" and ")
                .append(" ordertime>= ").append(startTime).append(" and ").append(" ordertime <= ").append(endTime).append(" and city=\'").append(city).append("\' ")
                .append(" group by shopName ").append(" limit ").append(page).append(" , ").append(size);

        List<ShopReportStatistics> shopReportStatisticsList = null;
        try {
            shopReportStatisticsList = (List<ShopReportStatistics>) DataDaoSql.getInstance().list(ShopReportStatistics.class, String.valueOf(sb));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return shopReportStatisticsList;
    }

    private long page(String city, String role, long startTime, Long endTime) {
        StringBuffer sb = new StringBuffer();
        List<ShopReportStatistics> shopReportStatisticsList = null;
        //SELECT COUNT(*) FROM shop_report_statistics WHERE shoprole =1
        /*sb.append("SELECT id FROM ").append(PojoTables.SHOP_REPORT_STATISTICS).append(" where shoprole=")
                .append(role).append(" and createtime>= ").append(starttime);*/
        sb.append("SELECT id,shopName,shopRole,SUM(successOrderCount) AS successOrderCount,SUM(ShopTurnover) AS shopTurnover,createtime,city,country FROM ")
                .append(PojoTables.SHOP_REPORT_STATISTICS).append(" where shoprole= ").append(role).append(" and ")
                .append(" ordertime>= ").append(startTime).append(" && ").append(" ordertime <= ").append(endTime).append(" and city=\'").append(city).append("\' ")
                .append(" group by shopName");
        try {
            shopReportStatisticsList = (List<ShopReportStatistics>) DataDaoSql.getInstance().list(ShopReportStatistics.class, String.valueOf(sb));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return shopReportStatisticsList.size();
    }
}